//==============================================================================
//
//        DLog - the singleton logger module in the ofc-library
//
//               Copyright (C) 2004  Dick van Oudheusden
//  
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2004-11-23 20:06:33 $ $Revision: 1.2 $
//
//==============================================================================

#include "ofc/DLog.h"


#if _INTERFACE_

#include "ofc/config.h"

#include "ofc/DLogger.h"

//
// Perfom logging of an event
// 
// @param level     the level of the event
// @param message   the message for the event
// 
// 
extern void dlog(int level, const char *message, ...);

//
// Set the mask for the level
// 
// @param levels    the bitwise ored levels
//
// @return the previous mask
// 

extern int dlogmask(int levels);

//
// Set the range for the level
// 
// @param high      the highest level to be masked
// @param low       the lowest level to be masked
// 
// @return the previous mask
// 
extern int dlogrange(int high, int low);

//
// Set the active logger
// 
// @param logger    the active logger (or nil for disable)
// 
// @return the previous logger
// 
extern id<DLogger> dlogger(id<DLogger> logger);

#endif


#if _PRIVATE_

//
// The singleton logger module 
// 

static id<DLogger> _logger = nil;

//
// Perfom logging of an event
// 
// @param level     the level of the event
// @param message   the message for the event
// 
// 
void dlog(int level, const char *message, ...)
{
  if (_logger != nil)
  {
    va_list ap;
    char    expanded[256];
    int     length;
    
    va_start(ap, message);
    length = vsnprintf(expanded, sizeof(expanded), message, ap);
    va_end(ap);

    if ((length > 0) && (length < sizeof(expanded)))
    {
      [_logger doLog :level :expanded];
    }
  }
}

//
// Set the mask for the level
// 
// @param levels    the bitwise ored levels
//
// @return the previous mask
// 

int dlogmask(int levels)
{
  int prev = 0;
    
  if (_logger != nil)
  {
    prev = [_logger mask :levels];
  }
  
  return prev;
}

//
// Set the range for the level
// 
// @param high      the highest level to be masked
// @param low       the lowest level to be masked
// 
// @return the previous mask
// 
int dlogrange(int high, int low)
{
  int prev = 0;
  
  if (_logger != nil)
  {
    prev = [_logger mask :high :low];
  }
  
  return prev;
}

//
// Set the active logger
// 
// @param logger    the active logger (or nil for disable)
// 
// @return the previous logger
// 
id<DLogger> dlogger(id<DLogger> logger)
{
  id  prev = _logger;
 
  _logger = logger;
  
  return prev;
}

#endif

/*==========================================================================*/
